package com.example.myapplication.rxjava;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.example.myapplication.R;
import com.example.myapplication.databinding.RxjavaretryactivityBinding;
import com.orhanobut.logger.Logger;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BooleanSupplier;
import io.reactivex.functions.Function;

/**
 * 类描述:
 * 创建人:   tangchao$
 * 创建时间: 2021/8/29$ 9:05 AM$
 * 联系方式: 419704299@qq.com
 * 修改人:   tangchao$
 * 修改时间: 2021/8/29$ 9:05 AM$
 * 修改备注:  [说明本次修改内容]
 */
public class RxjavaRetryActivity extends AppCompatActivity {
    RxjavaretryactivityBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.rxjavaretryactivity);
        test1();

    }

    int count = 0;

    private void test1() {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Object> emitter) throws Exception {
                emitter.onNext(114);
                emitter.onError(new Exception("404"));
//                emitter.onNext(2);

            }
        })
//                .retry(2)
//                .retryUntil(new BooleanSupplier() {
//                    @Override
//                    public boolean getAsBoolean() throws Exception {
//                        if (count < 5) {
//                            return true;
//                        } else {
//                            return false;
//                        }
//                    }
//                })
                .retryWhen(new Function<Observable<Throwable>, ObservableSource<?>>() {
                    @Override
                    public ObservableSource<?> apply(@NonNull Observable<Throwable> throwableObservable) throws Exception {
                        return throwableObservable.flatMap(new Function<Throwable, ObservableSource<?>>() {
                            @Override
                            public ObservableSource<?> apply(@NonNull Throwable throwable) throws Exception {
                                Logger.d("RxjavaRetryActivity===apply: "+throwable.getMessage());

                                if (throwable.getMessage().equals("404")) {
                                    return Observable.just("可以发送出去");
                                } else {
                                    return Observable.error(new Throwable("停止发送"));
                                }
                            }
                        });
                    }
                })
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull Object o) {
                        Logger.d("RxjavaRetryActivity===onNext: ");
                        binding.textView10.setText(o + "");
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Logger.e("RxjavaRetryActivity===: " + e.getMessage());

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
